<?php
namespace application\timer;
/*
 * @Create By: Vscode
 * @Author: falcon
 * @Date: 2021-02-16 22:57:27
 * 实际缺货列表数据缓存::优化改造
 */
use application\depend\Applog;
use application\depend\PhpRedis;
use application\model\PdoModel;

class PurchaseList
{

    //缺货列表
    public function main(){
        $startTime = time();
        //搜索 全部的 代采购状态的 订单详情
        $sql = "SELECT * FROM order_info WHERE status=2 and sales_return='' and alsobuy >0 GROUP BY buyer";
        $purchase = PdoModel::selectData($sql,[],'查询缺货列表错误 A1101');

        if($purchase === []){
            $err = [
                'title' => '没有数据呢',
                'sql' => $sql,
                'time' => date('Y-m-d H:i:s'),
            ];
            Applog::infoLog($err);
            //清空原来的
            PhpRedis::set('purchase','');
            PhpRedis::set('purchase_list','');
            return;
        }//if empty

        // 获取每个采购员的订单信息
        foreach($purchase as $key=>$val){
            $sql_purchase_list = "SELECT * FROM order_info WHERE status=2 and buyer='".$val['buyer']."' ORDER BY supplier asc,sku asc,order_number";

            $purchase_list = PdoModel::selectData($sql_purchase_list,[],'查询缺货列表错误 A1102');
            if(empty($purchase_list)){
                $err = [
                    'title' => 'Empty Data for Purchase_list',
                    'sql' => $sql_purchase_list,
                    'time' => date('Y-m-d H:i:s'),
                ];
                Applog::infoLog($err);
                return;
            }//if

            //新增  20191004    遍历 $purchase_list, 将每个$订单 de  sku 都合并好
            $purchase_list = combine_multi_sku($purchase_list); //合并同一订单下的同个SKU的 采购需求数
            foreach($purchase_list as $k=>$v){
                if($v['alsobuy'] <= 0){
                    unset($purchase_list[$k]);  //不显示 alsobuy = 0 的
                }
            }//foreach

            $purchase_list = array_values($purchase_list);
            
            // 循环获取订单内每个SKU详情信息
            foreach($purchase_list as $k=>$v){
                
                $sql_ring_filter = "SELECT product_type,default_styles,product_location,available_stock,goods_prepare_situation,cost_price FROM product WHERE id=".$v['p_id'];
                $ring_filter = PdoModel::selectData($sql_ring_filter,[],'查询缺货列表!A1238');
                if(empty($ring_filter)){
                    // 这里如果为空就要查下 配件表
                    $sql_access = "SELECT product_type,default_styles,product_location,available_stock,goods_prepare_situation,cost_price FROM product_acc WHERE id=".$v['p_id'];
                    $ring_filter = PdoModel::selectData($sql_access,[],'查询缺货列表!A1239');
                    if(empty($ring_filter)){
                        $err = [
                            'title' => '该SKU ID 无法定位！ --缺货列表定时任务！',
                        ];
                        Applog::infoLog($err,__FILE__,__LINE__);
                        return ;
                    }//if
                }// if
                
                $purchase_list[$k]['product_location'] = isset($ring_filter[0]['product_location']) ? $ring_filter[0]['product_location']: 'x';  //产品货位
                $purchase_list[$k]['available_stock'] = isset($ring_filter[0]['available_stock']) ? $ring_filter[0]['available_stock']: '0';  //产品库存数
                $purchase_list[$k]['goods_prepare_situation'] = isset($ring_filter[0]['goods_prepare_situation']) ? $ring_filter[0]['goods_prepare_situation']: '?';  //备货情况
                $purchase_list[$k]['cost_price'] = isset($ring_filter[0]['cost_price']) ? $ring_filter[0]['cost_price']:'0.00';  //多加 0  ;成本价

                //获取 总缺货数量
                $this->getStillNeedAmount($purchase_list[$k]);
                
                if(empty($purchase_list[$k]['count'])) unset($purchase_list[$k]);
              
            }//foreach 2
            $purchase_list = array_values($purchase_list);
            $this->unitTotalCount($purchase_list);// 这里是同步相同SKU不同订单下的 total_count , 保持都是最大值
            $purchase[$key] = $purchase_list;
        }//foreach

        
        /*将结果放入缓存*/
        PhpRedis::set('purchase',$purchase);
        PhpRedis::set('purchase_list',$purchase_list);

        $endTime = time();
        $costTime = ($endTime - $startTime) .'秒';
        Applog::infoLog(['title'=>'缺货列表统计花费时间','spend-time'=>$costTime,'date'=>date('Y-m-d H:i:s')]);
    }//func purchase_list

    //获取每个订单，SKU的缺货数量
    public function getStillNeedAmount(&$data){
        $waitPurchaseDetails = PhpRedis::get('distribut_stock_for_wait_purchase_order');
        if(empty($waitPurchaseDetails)) return;
        $data['total_count'] = 0;
        foreach($waitPurchaseDetails as $v){
            
            if(($v['sku'] == $data['sku']) && ($v['order_number'] == $data['order_number']) && $v['still_need']>0){
                $data['count'] = $v['still_need'];
            }
            //计算total count 总缺货数量
            if(($v['sku'] == $data['sku']) && $v['still_need']>0){
                $data['total_count'] += $v['still_need'];
            }
        }// foreach

        // 扫完了仍然没有 count 字段， 代表数据肯定有问题了
        if(!isset($data['count'])){
            // 还有一种情况： 库存数为0 且 仍然有需求 那么 still_need 就给 count 和 total_count  但是不在同一个缓存中. 
            $goToPurchase = PhpRedis::get('go_to_purchase_list');
            if(!empty($goToPurchase)){
                foreach($goToPurchase as $v){
                    if(($v['sku'] == $data['sku']) && ($v['order_number'] == $data['order_number']) && $v['still_need']>0){
                        $data['count'] = $v['still_need'];
                    }
                    //计算total count 总缺货数量
                    if(($v['sku'] == $data['sku']) && $v['still_need']>0){
                        $data['total_count'] += $v['still_need'];
                    }
                }//foreach
            }// if
        }//if
    }// func

    // 统计sku总缺货数
    public function unitTotalCount(&$data){
        $data2 = $data;
        foreach($data as &$v){
            $total_count = $v['total_count'];
            $v['total_count_all'] = $v['total_count'];
            foreach($data2 as $vv){
                if($v['sku'] == $vv['sku']){
                    // 比较 total_count; 较大的 保留
                    if($vv['total_count'] > $total_count) {
                        $v['total_count_all']=$vv['total_count'];
                    }
                }
            }
        }
    }
}//class
